package org.dromara.bykt.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.bykt.domain.bo.BYKTCourseBo;
import org.dromara.bykt.domain.vo.AliOSSProperties;
import org.dromara.bykt.domain.vo.BYKTCourseVo;
import org.dromara.bykt.service.IBYKTCourseService;
import org.dromara.bykt.utils.AliOSSUpload;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 课程
 *
 * @author Hou
 * @date 2025-06-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/bykt/course")
public class BYKTCourseController extends BaseController {

    private final IBYKTCourseService courseService;

    @Autowired
    private AliOSSUpload aliOSSUpload;
    /**
     * 查询课程列表
     */
    @GetMapping("/list")
    public TableDataInfo<BYKTCourseVo> list(BYKTCourseBo bo, PageQuery pageQuery) {
        return courseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出课程列表
     */
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
//        List<BYKTCourseVo> list = courseService.queryList(bo);
        ExcelUtil.exportExcel(null, "课程", BYKTCourseVo.class, response);
    }

    /**
     * 获取课程详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<BYKTCourseVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(courseService.queryById(id));
    }

    /**
     * 新增课程
     */
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@RequestBody BYKTCourseBo bo) throws Exception {
        return toAjax(courseService.insertByBo(bo));
    }

    /**
     * 修改课程
     */
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BYKTCourseBo bo) throws Exception {
        return toAjax(courseService.updateByBo(bo));
    }

    /**
     * 删除课程
     *
     * @param ids 主键串
     */
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(courseService.deleteWithValidByIds(List.of(ids), true));
    }

    @Log(title = "查询课程下拉")
    @GetMapping("/queryCourseList")
    public R<List<BYKTCourseVo>> queryCourseList() {
        List<BYKTCourseVo> byktCourseVos = courseService.queryCourseList();
        return R.ok(byktCourseVos);
    }

    @PostMapping("/queryCourseLists")
    public R<List<BYKTCourseVo>> queryCourseLists(@RequestBody BYKTCourseBo byktCourseBo) {
        return R.ok(courseService.queryCourseLists(byktCourseBo));
    }
}
